package com.etalk.crm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.Person;
import com.etalk.crm.service.StudentInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jordan
 */
@Controller
@RequestMapping(value = "/et")
public class OperationMaintenanceController {

    @Resource
    private StudentInfoService studentInfoService;

    /**
     * 获取问题人员提醒列表
     * @param model 页面数据对象
     * @param searchKey 查询关键字
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return 列表数据
     */
    @GetMapping("/monitor/list")
    public String getEtMonitoringList(Model model,String searchKey,Integer pageNum,Integer pageSize){
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=20;
        }
        model.addAttribute("searchKey",searchKey);
        model.addAttribute("pageInfo",studentInfoService.searchEtMonitoringListByPage(searchKey,pageNum,pageSize));
        return "et/et_monitor_list";
    }

    /**
     * 初始化添加页面
     * @param model 页面数据对象
     * @return 页面
     */
    @GetMapping("/monitor/init")
    public String initMonitorAdd(Model model){

        return "et/et_monitor_add";
    }

    /**
     * 提交问题用户监控信息
     * @param model 页面数据对象
     * @param personId 用户id
     * @param content 监控内容
     * @return 初始化页面
     */
    @PostMapping("/monitor/add")
    public String submitMonitorRecord(Model model, @RequestParam Integer personId, @RequestParam String content){
        int num=studentInfoService.addEtMonitorPerson(personId,content);
        model.addAttribute("status",num);
        return this.initMonitorAdd(model);
    }

    /**
     * 修改是否启用监控
     * @param personId 用户id
     * @param state 状态：1启用，0不启用
     * @return 记录数
     */
    @ResponseBody
    @PostMapping("/monitor/update/{state}")
    public String updateMonitorState(@RequestParam Integer personId, @PathVariable("state") Integer state){
        Map<String,Integer> resultMap=new HashMap<>(1);
        int num=studentInfoService.updateEtMonitorPersonByState(personId,state);
        resultMap.put("status",num);
        return JSON.toJSONString(resultMap);
    }

    /**
     * 修改监控内容
     * @param personId 用户id
     * @param content 监控内容
     * @return 记录数
     */
    @ResponseBody
    @PostMapping("/monitor/update/content")
    public String updateMonitorState(@RequestParam Integer personId, @RequestParam String content){
        Map<String,Integer> resultMap=new HashMap<>(1);
        int num=studentInfoService.updateEtMonitorPersonByContent(personId,content);
        resultMap.put("status",num);
        return JSON.toJSONString(resultMap);
    }

    /**
     * 删除监控记录
     * @param personId 用户id
     * @return 记录数
     */
    @ResponseBody
    @PostMapping("/monitor/del/{personId}")
    public String delMonitorInfo(@PathVariable("personId") Integer personId){
        Map<String,Integer> resultMap=new HashMap<>(1);
        int num=studentInfoService.delEtMonitorInfoByPersonId(personId);
        resultMap.put("status",num);
        return JSON.toJSONString(resultMap);
    }

    @RequestMapping(value = "/search/person/list", method = RequestMethod.POST)
    @ResponseBody
    public Object searchPersonList(String search){
        Map<String,Object> resultMap = new HashMap<>(2);
        List<Person> personList = studentInfoService.searchPersonListBySearchKey(search);
        resultMap.put("personList",personList);
        resultMap.put("code",1);
        return JSONObject.toJSON(resultMap);
    }
}
